package drds.binlog.parse.inbound;

import drds.binlog.event.TableMapLogEvent;

import java.util.ArrayList;
import java.util.List;

/**
 * 描述数据meta对象,mysql binlog中对应的{@linkplain TableMapLogEvent}包含的信息不全
 * <pre>
 * 1. 主键信息
 * 2. column name
 * 3. unsigned字段
 * </pre>
 */
public class TableMetaData
{

    private String schemaName;
    private String tableName;
    private List<FieldMetaData> fieldMetaDataList = new ArrayList<FieldMetaData>();
    private String ddl;                                          // 表结构的DDL语句

    public TableMetaData()
    {

    }

    public TableMetaData(String schemaName, String tableName, List<FieldMetaData> fieldMetaDataList)
    {
        this.schemaName = schemaName;
        this.tableName = tableName;
        this.fieldMetaDataList = fieldMetaDataList;
    }

    public String getFullName()
    {
        return schemaName + "." + tableName;
    }

    public String getSchemaName()
    {
        return schemaName;
    }

    public void setSchemaName(String schemaName)
    {
        this.schemaName = schemaName;
    }

    public String getTableName()
    {
        return tableName;
    }

    public void setTableName(String tableName)
    {
        this.tableName = tableName;
    }

    public List<FieldMetaData> getFieldMetaDataList()
    {
        return fieldMetaDataList;
    }

    public void setFieldMetaDataList(List<FieldMetaData> fileds)
    {
        this.fieldMetaDataList = fileds;
    }

    public FieldMetaData getFieldMetaData(String columnName)
    {
        for (FieldMetaData fieldMetaData : fieldMetaDataList)
        {
            if (fieldMetaData.getColumnName().equalsIgnoreCase(columnName))
            {
                return fieldMetaData;
            }
        }

        throw new RuntimeException("unknow column : " + columnName);
    }

    public List<FieldMetaData> getPrimaryKeyFieldMetaDataList()
    {
        List<FieldMetaData> fieldMetaDataList = new ArrayList<FieldMetaData>();
        for (FieldMetaData fieldMetaData : this.fieldMetaDataList)
        {
            if (fieldMetaData.isPrimaryKey())
            {
                fieldMetaDataList.add(fieldMetaData);
            }
        }

        return fieldMetaDataList;
    }

    public String getDdl()
    {
        return ddl;
    }

    public void setDdl(String ddl)
    {
        this.ddl = ddl;
    }

    public void addFieldMeta(FieldMetaData fieldMetaData)
    {
        this.fieldMetaDataList.add(fieldMetaData);
    }


}
